// $codepro.audit.disable numericLiterals
/**
 * Class: PATIENT PANEL
 * Description:
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/**
 * @author Team Awesome Express
 * @version $ Revision 1.0 $
 */
@SuppressWarnings("serial")
public class PatientPanel extends JPanel{
		 /**
		  * Field appointment.
		  */
		 private AppointmentFrame appointment;

		 /**
		  * Field myDataManager.
		  */
		 private DataManager myDataManager;

		 /**
		  * Field username.
		  */
		 private String username;

		 /**
		  * Field patientRow.
		  */
		 private final int[] patientRow;

		 /**
		  * Field patientArray.
		  */
		 private final Patient[] patientArray;

		 /**
		  * Field thePatient.
		  */
		 private final Patient thePatient;

		 /**
		  * Field confirm.
		  */
		 private ConfirmMsg confirm;

		 /**
		  * Field apptDate.
		  */
		 private String apptDate;

		 /**
		  * Field frame.
		  */
		 private static JFrame Frame;

	/**
	 * Create the patient panel.
	 * @param name String
	 * @param myDataManager DataManager
	 * @param username String
	 */
	public PatientPanel(String name, final String username, final DataManager myDataManager) {
		setBackground(GlobalVars.BACKGROUND);
		this.username = username;
		final String userlastname = name;
		this.myDataManager = myDataManager;
		
		patientRow = myDataManager.getRowNumbers(GlobalVars.PATIENT_SHEET, 1, username); // Find row number in database of patient
		patientArray = getPatient(patientRow);
		thePatient = patientArray[0]; // thePatient holds the patient(object) that logged in
		
		final JButton btnAppointment = new JButton("Request an appointment");
		btnAppointment.setBounds(134, 145, 197, 29);
		final JButton btnMedHistory = new JButton("View medical history");
		btnMedHistory.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) { // View Medical History Button is pressed
				PatientInformationPanel.createWindow(myDataManager, thePatient); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			}
		});
		btnMedHistory.setBounds(134, 185, 197, 29);
		setLayout(null);
		add(btnAppointment);
		add(btnMedHistory);
		
		final JLabel welcomeLabel = new JLabel("Welcome " + userlastname);
		welcomeLabel.setBounds(37, 18, 181, 29);
		add(welcomeLabel);
		
		final JLabel headerLabel = new JLabel("What would you like to do?");
		headerLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		headerLabel.setForeground(new Color(0, 0, 0));
		headerLabel.setBounds(134, 91, 197, 23);
		add(headerLabel);
		
		final JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent arg0) {
				Frame.dispose(); // Close Patient Panel
				LoginFrame.createWindow(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			}
		});
		btnLogOut.setBounds(134, 232, 197, 29);
		add(btnLogOut);
		btnAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {		// For Request Appointment Button
				//Check whether patient has appointment already
				boolean hasAppt = false;
				apptDate = "";
				final String[] apptList = myDataManager.getDataAtCol(GlobalVars.APPOINTMENT_SHEET, "Username*", "Username*");
				for(int i=0; i < apptList.length; i++){
					if(apptList[i].equals(username)){
						hasAppt = true;
						String[] dateList = myDataManager.getDataAtCol(GlobalVars.APPOINTMENT_SHEET, "Date", "Date");
						apptDate = dateList[i];
					}
				}
				
				if(hasAppt){
					confirm = new ConfirmMsg();
				}
				else{
					appointmentButtonPressed(userlastname);
				}
			}
		});
	}
	
	/**
	 * Method appointmentButtonPressed.
	 * @param username String
	 */
	public void appointmentButtonPressed(String username) {
		appointment = new AppointmentFrame(myDataManager, GlobalVars.PATIENT_USERTYPE);
	}
	
	/**
	 * Method getPatient.
	 * @param rowNumbers int[]
	 * @return Patient[]
	 */
	private Patient[] getPatient(int[] rowNumbers){
		final Patient[] patientsArray = new Patient[rowNumbers.length];
		final String[][] rawData = new String[rowNumbers.length][];
		for (int i = 0; i < rowNumbers.length; i++){
			rawData[i] = myDataManager.getDataAtRow(
						GlobalVars.PATIENT_SHEET,
						1,
						12,
						rowNumbers[i]
						 );
			patientsArray[i] = new Patient(rawData[i]); 
		}
		return patientsArray;
	}
	
	/**
	 * Method createWindow.
	 * @param name String
	 * @param myDataManager DataManager
	
	 * @param username String
	 * @return JFrame */
	public static JFrame createWindow(String name, String username, DataManager myDataManager) {
	    Frame = new JFrame();
		Frame.setSize(450, 300);
		Frame.setTitle("Welcome " + name);
		Frame.getContentPane().add(new PatientPanel(name, username, myDataManager));
		Frame.setVisible(true);
		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return Frame;
	}
	
	/**
	 * Warning Message Pane for missing information
	 * @author Marcus
	 */
	public class ConfirmMsg{
		/**
		 * Constructor for WarningMsg.
		 */
		public ConfirmMsg(){
			
			final String msg="Already has an appointment on " + apptDate + ".\n" +
						"Would like to cancel existing appointment?";
			final int btPressed = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
			//When yes is pressed
			if(btPressed == 0){
				myDataManager.deleteRow(GlobalVars.APPOINTMENT_SHEET, username);
			}
			//JOptionPane.showMessageDialog(null, msg, "Warning", JOptionPane.QUESTION_MESSAGE); //$codepro.audit.disable lineLength
		}
	}
}
